<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Excel文件读取器</title>
    <script src="https://mirror.fe80.cn/xlsx/0.18.5/xlsx.full.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            padding: 20px;
            color: #333;
        }
        
        .container {
            max-width: 1300px;
            margin: 0 auto;
            padding: 20px;
        }
        
        header {
            text-align: center;
            margin-bottom: 30px;
            padding: 20px;
            background: rgba(255, 255, 255, 0.8);
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        h1 {
            color: #2c3e50;
            margin-bottom: 10px;
            font-size: 2.2rem;
        }
        
        .description {
            color: #7f8c8d;
            max-width: 800px;
            margin: 0 auto;
            line-height: 1.6;
        }
        
        .upload-container {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 5px 25px rgba(0, 0, 0, 0.1);
            margin-bottom: 30px;
            text-align: center;
        }
        
        .upload-area {
            border: 3px dashed #3498db;
            border-radius: 12px;
            padding: 40px 20px;
            margin: 20px 0;
            background: #f8f9fa;
            transition: all 0.3s;
            cursor: pointer;
        }
        
        .upload-area:hover {
            background: #e3f2fd;
            border-color: #2980b9;
        }
        
        .upload-icon {
            font-size: 60px;
            color: #3498db;
            margin-bottom: 15px;
        }
        
        .upload-text {
            font-size: 18px;
            color: #7f8c8d;
            margin-bottom: 15px;
        }
        
        .file-input {
            display: none;
        }
        
        .btn {
            background: #3498db;
            color: white;
            border: none;
            padding: 12px 25px;
            font-size: 16px;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s;
            display: inline-block;
            margin-top: 15px;
        }
        
        .btn:hover {
            background: #2980b9;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .result-container {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 5px 25px rgba(0, 0, 0, 0.1);
            display: none;
        }
        
        .result-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f1f1f1;
        }
        
        .result-title {
            font-size: 1.5rem;
            color: #2c3e50;
        }
        
        .data-count {
            background: #3498db;
            color: white;
            padding: 5px 15px;
            border-radius: 20px;
            font-size: 14px;
        }
        
        .table-container {
            overflow-x: auto;
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            min-width: 1000px;
        }
        
        th {
            background: #3498db;
            color: white;
            padding: 15px;
            text-align: left;
            position: sticky;
            top: 0;
        }
        
        td {
            padding: 12px 15px;
            border-bottom: 1px solid #eee;
        }
        
        tr:nth-child(even) {
            background-color: #f8f9fa;
        }
        
        tr:hover {
            background-color: #e3f2fd;
        }
        
        .subject-cell {
            text-align: center;
            font-weight: bold;
        }
        
        .error-message {
            background: #ffebee;
            color: #c62828;
            padding: 15px;
            border-radius: 8px;
            margin: 20px 0;
            display: none;
        }
        
        .loading {
            text-align: center;
            padding: 30px;
            display: none;
        }
        
        .spinner {
            border: 5px solid #f3f3f3;
            border-top: 5px solid #3498db;
            border-radius: 50%;
            width: 50px;
            height: 50px;
            animation: spin 1s linear infinite;
            margin: 0 auto 20px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        @media (max-width: 768px) {
            .container {
                padding: 10px;
            }
            
            .upload-container, .result-container {
                padding: 20px;
            }
            
            h1 {
                font-size: 1.8rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>Excel文件读取器</h1>
            <p class="description">上传Excel文件，自动解析学生成绩数据。支持.xlsx和.xls格式文件。</p>
        </header>
        
        <div class="upload-container" id="upload-container">
            <div class="upload-area" id="dropArea">
                <div class="upload-icon">📊</div>
                <p class="upload-text">拖放Excel文件到此处或点击上传</p>
                <input type="file" id="fileInput" class="file-input" accept=".xlsx, .xls">
                <button class="btn" id="uploadBtn">选择文件</button>
            </div>
        </div>
        
        <div class="error-message" id="errorMessage"></div>
        
        <div class="loading" id="loading">
            <div class="spinner"></div>
            <p>正在解析Excel文件，请稍候...</p>
        </div>
        
        <div class="result-container" id="resultContainer">
            <div class="result-header">
                <h2 class="result-title">学生成绩数据</h2>
                <div class="data-count" id="dataCount">0条记录</div>
            </div>
            
            <div class="table-container">
                <table id="dataTable">
                    <thead>
                        <tr>
                            <th>姓名</th>
                            <th>班级</th>
                            <th>考号</th>
                            <th>语文</th>
                            <th>数学</th>
                            <th>英语</th>
                            <th>历史</th>
                            <th>地理</th>
                            <th>物理</th>
                            <th>生物</th>
                            <th>道法</th>
                            <th>计分科目</th>
                            <th>计分类别</th>
                        </tr>
                    </thead>
                    <tbody id="tableBody">
                        <!-- 数据将通过JavaScript填充 -->
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const fileInput = document.getElementById('fileInput');
            const uploadBtn = document.getElementById('uploadBtn');
            const dropArea = document.getElementById('dropArea');
            const resultContainer = document.getElementById('resultContainer');
            const tableBody = document.getElementById('tableBody');
            const dataCount = document.getElementById('dataCount');
            const errorMessage = document.getElementById('errorMessage');
            const loading = document.getElementById('loading');
            
            // 点击上传按钮触发文件选择
            uploadBtn.addEventListener('click', function() {
                fileInput.click();
            });
            
            // 文件选择变化事件
            fileInput.addEventListener('change', function(e) {
                if (e.target.files.length) {
                    handleFile(e.target.files[0]);
                }
            });
            
            // 拖放事件处理
            dropArea.addEventListener('dragover', function(e) {
                e.preventDefault();
                dropArea.style.borderColor = '#2980b9';
                dropArea.style.backgroundColor = '#e3f2fd';
            });
            
            dropArea.addEventListener('dragleave', function() {
                dropArea.style.borderColor = '#3498db';
                dropArea.style.backgroundColor = '#f8f9fa';
            });
            
            dropArea.addEventListener('drop', function(e) {
                e.preventDefault();
                dropArea.style.borderColor = '#3498db';
                dropArea.style.backgroundColor = '#f8f9fa';
                
                if (e.dataTransfer.files.length) {
                    handleFile(e.dataTransfer.files[0]);
                }
            });
            
            // 处理Excel文件
            function handleFile(file) {
                // 验证文件类型
                const validTypes = [
                    'application/vnd.ms-excel',
                    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                    'application/vnd.ms-excel.sheet.macroEnabled.12'
                ];
                
                if (!validTypes.includes(file.type) && 
                    !file.name.match(/\.(xlsx|xls)$/i)) {
                    showError('请上传有效的Excel文件 (.xlsx 或 .xls 格式)');
                    return;
                }
                
                // 显示加载状态
                loading.style.display = 'block';
                resultContainer.style.display = 'none';
                errorMessage.style.display = 'none';
                
                const reader = new FileReader();
                
                reader.onload = function(e) {
                    try {
                        const data = new Uint8Array(e.target.result);
                        const workbook = XLSX.read(data, {type: 'array'});
                        
                        // 获取第一个工作表
                        const firstSheetName = workbook.SheetNames[0];
                        const worksheet = workbook.Sheets[firstSheetName];
                        
                        // 将工作表转换为JSON
                        const jsonData = XLSX.utils.sheet_to_json(worksheet, {header: 1});
                        
                        // 处理数据
                        processExcelData(jsonData);
                    } catch (error) {
                        showError('解析Excel文件时出错: ' + error.message);
                        loading.style.display = 'none';
                    }
                };
                
                reader.onerror = function() {
                    showError('读取文件时出错');
                    loading.style.display = 'none';
                };
                
                reader.readAsArrayBuffer(file);
            }
            
            // 处理Excel数据
            function processExcelData(data) {
                // 隐藏加载状态
                loading.style.display = 'none';
                
                // 清空错误消息
                errorMessage.style.display = 'none';
                
                // 检查数据是否有效
                if (!data || data.length < 2) {
                    showError('Excel文件中没有有效数据');
                    return;
                }
                
                // 获取表头
                const headers = data[0];
                
                // 验证表头
                const expectedHeaders = ['姓名', '班级', '考号', '语文', '数学', '英语', 
                                        '历史', '地理', '物理', '生物', '道法', '计分学科', '计分类别'];
                
                let validHeaders = true;
                expectedHeaders.forEach((header, index) => {
                    if (headers[index] !== header) {
                        console.log(header, headers[index]);
                        validHeaders = false;
                    }
                });
                
                if (!validHeaders) {
                    showError('Excel表头格式不符合要求，请使用提供的模板');
                    return;
                }
                
                // 清空表格
                tableBody.innerHTML = '';
                
                // 处理数据行
                const dataRows = data.slice(1);
                let rowCount = 0;
                
                dataRows.forEach(row => {
                    // 跳过空行
                    if (row.length === 0 || row.every(cell => cell === '')) return;
                    
                    const tr = document.createElement('tr');
                    
                    // 创建表格单元格
                    row.forEach((cell, index) => {
                        const td = document.createElement('td');
                        
                        // 处理科目成绩单元格
                        if (index >= 3 && index <= 10) {
                            td.className = 'subject-cell';
                            // 格式化成绩显示
                            td.textContent = typeof cell === 'number' ? cell.toFixed(1) : cell;
                        } else {
                            td.textContent = cell;
                        }
                        
                        tr.appendChild(td);
                    });
                    
                    tableBody.appendChild(tr);
                    rowCount++;
                });
                
                // 更新数据计数
                dataCount.textContent = `${rowCount}条记录`;
                
                // 显示结果
                resultContainer.style.display = 'block';

                // 隐藏上传框
                document.getElementById("upload-container").style.display = 'none';
            }
            
            // 显示错误消息
            function showError(message) {
                errorMessage.textContent = message;
                errorMessage.style.display = 'block';
                resultContainer.style.display = 'none';
                loading.style.display = 'none';
            }
        });
    </script>
</body>
</html>
